package com.song.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.request.ApCookbookIngredientRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.song.module.admin.mapper.ApCookbookIngredientMapper;
import org.springframework.beans.BeanUtils;
import com.song.module.admin.entity.ApCookbookIngredient;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.song.module.admin.service.IApCookbookIngredientService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜谱用料业务层处理
 *
 * @author song
 * @date 2024-02-24
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApCookbookIngredientServiceImpl implements IApCookbookIngredientService {

    private final ApCookbookIngredientMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApCookbookIngredient add(ApCookbookIngredientRequest request) {
        ApCookbookIngredient entity = ApCookbookIngredient.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApCookbookIngredientRequest request) {
        ApCookbookIngredient entity = ApCookbookIngredient.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public ApCookbookIngredient getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<ApCookbookIngredient> pageList(ApCookbookIngredientRequest request) {
        Page<ApCookbookIngredient> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApCookbookIngredient> query = this.buildWrapper(request);
        IPage<ApCookbookIngredient> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<ApCookbookIngredient> list(ApCookbookIngredientRequest request) {
        LambdaQueryWrapper<ApCookbookIngredient> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<ApCookbookIngredient> buildWrapper(ApCookbookIngredientRequest param) {
        LambdaQueryWrapper<ApCookbookIngredient> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getName())){
               query.like(ApCookbookIngredient::getName ,param.getName());
        }
         if (StringUtils.isNotBlank(param.getAmount())){
               query.like(ApCookbookIngredient::getAmount ,param.getAmount());
        }
        return query;
    }

}
